package be.vdab.servlets;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import be.vdab.dao.PizzaDAO;
import be.vdab.entities.Pizza;
import be.vdab.util.RequestStatistiek;

/**
 * Servlet implementation class VoorkeurPizzaServlet
 */
@WebServlet("/pizzas/voorkeuren.htm")
public class VoorkeurPizzaServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private static final String INPUT_VIEW = "/WEB-INF/JSP/voorkeurpizzasvragen.jsp";
	private static final String SUCCESS_VIEW = "/WEB-INF/JSP/voorkeurpizzastonen.jsp";
    private PizzaDAO pizzaDAO = new PizzaDAO(); 
    private final RequestStatistiek requestStatistiek = new RequestStatistiek(); 
   
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String view;
	    if (request.getParameterValues("nummer") == null) {
	    	request.setAttribute("pizzas", pizzaDAO.findAll());
		    view = INPUT_VIEW;
	    } else {
	    	List<Pizza> pizzas = new ArrayList<>();
	    	try {
	    		for (String nummerAlsString:request.getParameterValues("nummer")){
	    			long nummer = Long.parseLong(nummerAlsString);
    				Pizza pizza = pizzaDAO.read(nummer);
    				if (pizza != null) {
						pizzas.add(pizza);
    				}
	    		}
	        request.setAttribute("pizzas", pizzas);
	        view = SUCCESS_VIEW;
	    	} catch (NumberFormatException ex) {
	    		request.setAttribute("pizzas", pizzaDAO.findAll());
	    		view = INPUT_VIEW;
	    	}
	    }
	    requestStatistiek.voegRequestToe(request);
	    RequestDispatcher dispatcher = request.getRequestDispatcher(view);
	    dispatcher.forward(request, response);
 	}
}
